package com.congee02.bt.dfs.top2bottom;

import com.congee02.bt.TreeNode;

public class LongestZigZag {

    public int longestZigZag(TreeNode root) {

        if (root == null) {
            return 0;
        }

        process(root, false, 0);
        process(root, true, 0);

        return ans;
    }

    int ans = 0;
    private void process(TreeNode root, boolean left, int len) {
        if (root == null) {
            return;
        }

        ans = Math.max(ans, len);

        process(left ? root.left : root.right, ! left, len + 1);
        process(left ? root.right : root.left, left, 1);
    }

}
